package com.jingxi.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingxi.common.pojo.EasyUIDataGridResult;
import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.mapper.TbItemParamMapper;
import com.jingxi.model.TbItemParam;
import com.jingxi.model.TbItemParamExample;

@Service
public class ItemParamServiceImpl implements ItemParamService {

	@Autowired
	private TbItemParamMapper itemParamMapper;

	@Override
	public EasyUIDataGridResult getItemParamList(int page, int rows) {
		TbItemParamExample example = new TbItemParamExample();
		// 分页处理
		PageHelper.startPage(page, rows);
		List<TbItemParam> list = itemParamMapper.selectByExampleWithBLOBs(example);
		// 创建一个返回值对象
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		// 取记录总条数
		PageInfo<TbItemParam> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public JingXiResult getParamFromItemCatId(long id) {
		TbItemParamExample example = new TbItemParamExample();
		example.createCriteria().andItemCatIdEqualTo(id);
		List<TbItemParam> tbItemParams = itemParamMapper.selectByExampleWithBLOBs(example);
		if(tbItemParams.size()==1)
			return JingXiResult.ok(tbItemParams.get(0));
		return JingXiResult.ok();
	}

	/**
	 * 添加商品规格参数
	 */
	@Override
	public JingXiResult addItemParam(long cid, String paramData) {
		Date date = new Date();
		TbItemParam tbItemParam = new TbItemParam();
		tbItemParam.setItemCatId(cid);
		tbItemParam.setParamData(paramData);
		tbItemParam.setCreated(date);
		tbItemParam.setUpdated(date);
		
		itemParamMapper.insert(tbItemParam);
		return JingXiResult.ok();
	}
	
}
